package com.rongshu.view.home.fragment.dialog;

import android.app.Dialog;
import android.content.Context;
import android.support.v4.content.ContextCompat;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.request.RequestOptions;
import com.rongshu.R;
import com.rongshu.utils.Common;
import com.rongshu.utils.SpannableStringUtils;
import com.rongshu.view.home.adapter.PeopleRecommendAdapter;
import com.rongshu.view.home.contract.PeopleRecommendView;
import com.rongshu.view.home.entity.PeopleRecommendRespose;
import com.rongshu.view.home.presenter.PeopleRecommendPresenter;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.listener.OnLoadmoreListener;
import com.scwang.smartrefresh.layout.listener.OnRefreshListener;
import com.trello.rxlifecycle.LifecycleTransformer;
import com.widget.CircleImageView;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class PeopleRecommendDialog extends Dialog {
    CircleImageView iv_head_icon;
    TextView txt_user_name;
    SmartRefreshLayout smartRefreshLayout;
    RecyclerView recyclerView;


    public PeopleRecommendDialog(Context context) {
        super(context, R.style.full_width_dialog_at_screen_bottom);
        setContentView(R.layout.fragment_people_recommend_dialog_view);
        initView();
    }

    private void initView() {
        iv_head_icon = findViewById(R.id.iv_head_icon);
        txt_user_name = findViewById(R.id.txt_user_name);
        smartRefreshLayout = findViewById(R.id.smartRefreshLayout);
        recyclerView = findViewById(R.id.recyclerView);
        findViewById(R.id.iv_close).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                dismiss();
            }
        });
        WindowManager.LayoutParams layoutParams = this.getWindow().getAttributes();
        layoutParams.gravity = Gravity.BOTTOM | Gravity.CENTER_HORIZONTAL;
        layoutParams.width = ViewGroup.LayoutParams.MATCH_PARENT;
        layoutParams.height = ViewGroup.LayoutParams.WRAP_CONTENT;
        setCanceledOnTouchOutside(true);
    }

    public static class Builder implements PeopleRecommendView {
        private Context context;
        private PeopleRecommendDialog dialog;
        //当前文章ID,(1文章，2连载),推荐人ID
        private int contribute_id, contribute_type, from_uid;
        //avatar,from_Nmae,TotalCount
        private String from_Nmae;
        private String avatar;
        private int totalCount;
        //map
        private Map<String, Object> map = null;
        private int pageIndex = 1;
        private int pageSize = 10;
        //manager
        private LinearLayoutManager manager = null;
        //adapter
        private PeopleRecommendAdapter mPeopleRecommendAdapter = null;
        //presenter
        private PeopleRecommendPresenter presenter = null;
        //refresh,load
        private boolean isrefresh = true;

        public Builder(Context context) {
            this.context = context;
        }

        public Builder setContribute_id(int id) {
            this.contribute_id = id;
            return this;
        }

        public Builder setContribute_type(int type) {
            this.contribute_type = type;
            return this;
        }

        public Builder setFrom_uid(int from_uid) {
            this.from_uid = from_uid;
            return this;
        }

        public Builder setAvatar(String avatar) {
            this.avatar = avatar;
            return this;
        }

        public Builder setFrom_Nmae(String from_Nmae) {
            this.from_Nmae = from_Nmae;
            return this;
        }

        public Builder setTotalCount(int totalCount) {
            this.totalCount = totalCount;
            return this;
        }

        public PeopleRecommendDialog create() {
            if (dialog == null) {
                dialog = new PeopleRecommendDialog(context);
            }
            //presenter
            presenter = new PeopleRecommendPresenter(context);
            presenter.attachView(this);
            //avatar
            RequestOptions options = new RequestOptions()
                    .placeholder(R.drawable.user_head_icon);
            Glide.with(context)
                    .load(avatar)
                    .apply(options)
                    .into(dialog.iv_head_icon);
            //name
            if (totalCount > 1) {
                String dealContent = from_Nmae + " (" + totalCount + ")";
                dialog.txt_user_name.setText(SpannableStringUtils.addForeColorSpan(dealContent,
                        ContextCompat.getColor(context, R.color.color_3cc051), from_Nmae.length(), null));
            } else {
                dialog.txt_user_name.setText(from_Nmae);
            }
            //manager
            manager = new LinearLayoutManager(context);
            //adapter
            mPeopleRecommendAdapter = new PeopleRecommendAdapter();
            //recyclerview
            dialog.recyclerView.setLayoutManager(manager);
            dialog.recyclerView.setAdapter(mPeopleRecommendAdapter);
            //refresh
            dialog.smartRefreshLayout.setOnRefreshListener(new OnRefreshListener() {
                @Override
                public void onRefresh(RefreshLayout refreshlayout) {
                    isrefresh = true;
                    pageIndex = 1;
                    refreshlayout.resetNoMoreData();
                    getOneRecommendMsg();
                }
            });

            dialog.smartRefreshLayout.setOnLoadmoreListener(new OnLoadmoreListener() {
                @Override
                public void onLoadmore(RefreshLayout refreshlayout) {
                    isrefresh = false;
                    pageIndex++;
                    getOneRecommendMsg();
                }
            });
            dialog.smartRefreshLayout.autoRefresh();
            return dialog;
        }

        /**
         * 获取个人推荐语列表
         */
        private void getOneRecommendMsg() {
            map = new HashMap<>();
            map.put("contribute_id", contribute_id);
            map.put("contribute_type", contribute_type);
            map.put("from_uid", from_uid);
            map.put("pageIndex", pageIndex);
            map.put("pageSize", pageSize);
            presenter.getOneRecommendMsg(contribute_id, contribute_type,
                    from_uid, pageIndex, pageSize, Common.encryptionSign(map));
        }

        @Override
        public void onPeopleRecommend(List<PeopleRecommendRespose.ResponseBean> responseBeans) {
            if (mPeopleRecommendAdapter != null) {
                if (isrefresh) {
                    dialog.smartRefreshLayout.finishRefresh();
                    mPeopleRecommendAdapter.setResponseBeanList(responseBeans);
                    mPeopleRecommendAdapter.notifyDataSetChanged();
                } else {
                    if (responseBeans == null || responseBeans.isEmpty()) {
                        dialog.smartRefreshLayout.finishLoadmoreWithNoMoreData();
                    } else {
                        dialog.smartRefreshLayout.finishLoadmore();
                        mPeopleRecommendAdapter.addResponseBeanList(responseBeans);
                        mPeopleRecommendAdapter.notifyDataSetChanged();
                    }
                }
            }
        }

        @Override
        public <T> LifecycleTransformer<T> bindToLife() {
            return null;
        }
    }
}
